var twitter_theme = window.twitter_theme || {} ;

try { console.log('init console... done') ; } catch(e) { console = { log: function() {} } }

$(document).ready(function(){
    // Dropdown languages
    // ===============================
    $(".login_nav ul li.languages").hover(
        function(){
            $(this).find("ul").show();
        },
        function(){
            $(this).find("ul").hide();
        }
    );
	// Dropdown topbar nav
    // ===============================
    $("body").bind("click", function (e) {
        $('a.menu').parent("li").removeClass("open");
    });
    $("a.menu").click(function (e) {
		$('a.menu').parent("li").removeClass("open");
        var $li = $(this).parent("li").toggleClass('open');
        return false;
    });
    // Close alerts
    // ===============================
    $(".alert-message .close").bind("click", function(e) {
       $(this).parent().fadeOut('slow'); 
    });
    // Close item contact modal
    // ===============================
    $(".item-contact .close").bind("click", function(e) {
       $(this).parent().parent().parent().fadeOut('slow'); 
    });
    $(".item-sendfriend .close").bind("click", function(e) {
       $(this).parent().parent().parent().fadeOut('slow'); 
    });
    // Select with choosen
    // ===============================
    //$(".chzn-select").chosen();
});

function check_validation(element) {
   
     if( !$(element).is(':checked')) {
        if ( $(element).parent().parent().hasClass('error') == false ) {
            $(element).parent().parent().addClass('error') ;
            var span = $("<span>").attr('class', 'help-inline').html(text_error_required);
            $(element).parent().append(span) ;
        }
        return false;
    }
    return true;
}

function text_validation (element) {
    if( $(element).val().length == 0 ) {
        if ( $(element).parent().parent().hasClass('error') == false ) {
            $(element).parent().parent().addClass('error') ;
            var span = $("<span>").attr('class', 'help-inline').html(text_error_required);
            $(element).parent().append(span) ;
        }
        return false;
    }
    return true;
}

function mail_validation (element) {
    if( $(element).val().length == 0 ) {
        if ( $(element).parent().parent().hasClass('error') == false ) {
            $(element).parent().parent().addClass('error') ;
            var span = $("<span>").attr('class', 'help-inline').html(text_error_required);
            $(element).parent().append(span) ;
        }
        return false ;
    } else if ( !valid_email($(element).val()) ) {
        if ( $(element).parent().parent().hasClass('error') == false ) {
            $(element).parent().parent().addClass('error') ;
            var span = $("<span>").attr('class', 'help-inline').html(text_valid_email);
            $(element).parent().append(span) ;
        }
        return false ;
    }
    return true ;
}

function removeError(element) {
   element.parent().parent().removeClass('error') ;
   element.parent().find(".help-inline").remove() ;
}

function valid_email( email ) {
    return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(email);
}

//Juanjo guarrerias
function valButton(btn) {
    var cnt = -1;
    for (var i = btn.length - 1; i > -1; i--) {
        if (btn[i].checked) { cnt = i; i = -1; }
    }
    if (cnt > -1) return btn[cnt].value;
    else return null;
}

function showPanel(show, hide)
{
    $('#'+hide).fadeOut(1000, function () {
        $('#'+show).fadeIn(600);});
    $(window).scrollTop(0);
}
function VeicleTypeWrapper(type)
	{
		var result;
		
		switch(type)
		{
                    case "1": result = 1; break;
                    case "2": result = 2;break;
                    case "3": result = 3;break;
		}
		
		return result;
	}
	
function Loading()
{
	$('#curtain').css('display','inline');
	$('#clock').css('display','inline');
}

function HideLoading()
{
	$('#curtain').css('display','none');
	$('#clock').css('display','none');
}

function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}

function numericInput(id)
{
  $("#"+id).bind("keyup paste", function(){
            setTimeout(jQuery.proxy(function() {
                this.val(this.val().replace(/[^0-9]/g, ''));
            }, $(this)), 0);
      });
}

function numericInputDot(id)
{
  $("#"+id).bind("keyup paste", function(){
            setTimeout(jQuery.proxy(function() {
                this.val(this.val().replace(/[^0-9.]/g, ''));
            }, $(this)), 0);
      });
}